﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraEditors.Controls;

namespace QuangNgai.nhaplieu.tudien
{
    public partial class frmTuDien : DevExpress.XtraEditors.XtraForm
    {
        private LTQNLinQDataContext db;
        public frmTuDien()
        {
            InitializeComponent();
            db = new LTQNLinQDataContext();
        }

        private void frmTuDien_Load(object sender, EventArgs e)
        {
            this.dictionaryTableAdapter.Fill(this.lTQNDataSet.Dictionary);

            // init txtEditChiMuc Binding props
            this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.dictionaryBindingSource, "IndexName", true));
            this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.dictionaryBindingSource, "ID", true));
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtTenChiMuc.Text == "")
            {
                MessageBox.Show("Tên chỉ mục không được trống", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                int parentID = 0;


                if (chkMucGoc.Checked == true)
                {
                    parentID = 0;
                }
                else
                {
                    parentID = (int)txtMucCha.Tag;
                }
                LTQNDataSet.DictionaryRow newDic = (LTQNDataSet.DictionaryRow)((DataRowView)dictionaryBindingSource.AddNew()).Row;
                newDic.IndexName = txtTenChiMuc.Text;
                newDic.ParentID = parentID;

                dictionaryBindingSource.EndEdit();
                dictionaryTableAdapter.Update(lTQNDataSet.Dictionary);

                MessageBox.Show("Thêm chỉ mục thành công", "Thêm chỉ mục thành công",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTenChiMuc.Text = "";
            }
            catch (Exception)
            {
                //throw;
                MessageBox.Show("Không thể thêm chỉ mục", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void chkMucGoc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMucGoc.Checked == true)
            {
                lblMucCha.Visible = txtMucCha.Visible = false;
            }
            else
            {
                lblMucCha.Visible = txtMucCha.Visible = true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Xác nhận xóa chỉ mục: " + txtMucCha.Text, "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
            if (!result.Equals(DialogResult.Yes))
            {
                return;
            }

            //Dictionary dic = db.Dictionaries.Single(d => d.ID == (int)txtMucCha.Tag);
            //db.Dictionaries.DeleteOnSubmit(dic);
            //db.SubmitChanges();

            //// refill the underlying datasource of tree list
            //this.dictionaryTableAdapter.Fill(this.lTQNDataSet.Dictionary);
            dictionaryBindingSource.RemoveCurrent();
            dictionaryTableAdapter.Update(lTQNDataSet.Dictionary);
            txtTenChiMuc.Text = "";
            //dictionaryBindingSource.Position = 0;
        }

        private void treeTuDien_DragDrop(object sender, DragEventArgs e)
        {
            dictionaryBindingSource.EndEdit();
            dictionaryTableAdapter.Update(lTQNDataSet.Dictionary);
            Console.WriteLine("aaaaaaaaaaa");
            this.dictionaryTableAdapter.Fill(this.lTQNDataSet.Dictionary);

        }

        private void rgEditChoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtEditChiMuc.DataBindings.Clear();
            txtEditMucCha.DataBindings.Clear();
            if (rgEditChoice.SelectedIndex == 0)
            {
                txtEditChiMuc.EditValue = null;
                txtEditChiMuc.Tag = null;
                //    this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.dictionaryBindingSource, "IndexName", true));
                //    this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.dictionaryBindingSource, "ID", true));
            }
            else if (rgEditChoice.SelectedIndex == 1)
            {
                txtEditMucCha.Tag = txtEditMucCha.EditValue = null;
                //    this.txtEditMucCha.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.dictionaryBindingSource, "IndexName", true));
                //    this.txtEditMucCha.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.dictionaryBindingSource, "ID", true));
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            if (txtEditChiMuc.Tag==null  || Convert.ToInt32(txtEditChiMuc.Tag)==-1 
                || ((txtEditMucCha.Tag==null ||Convert.ToInt32(txtEditMucCha.Tag)==-1)&& !chkEditMucGoc.Checked))
            {
                MessageBox.Show("Chưa chọn chỉ mục hiện tại hoặc chỉ mục cha/gốc", "Không thể cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Console.WriteLine(txtEditChiMuc.Tag + "," + txtEditMucCha.Tag);
            var i = dictionaryBindingSource.Find("ID", txtEditChiMuc.Tag);
            LTQNDataSet.DictionaryRow currDic = (LTQNDataSet.DictionaryRow)((DataRowView)dictionaryBindingSource[i]).Row;
            if (chkEditMucGoc.Checked)
            {
                currDic.ParentID = 0;
            }
            else
            {
                currDic.ParentID = Convert.ToInt32(txtEditMucCha.Tag);
            }
            if (txtEditChiMuc.Text.Length == 0)
            {
                MessageBox.Show("Tên chỉ mục không được rỗng", "Không thể cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                currDic.IndexName = txtEditChiMuc.Text;
            }
            dictionaryTableAdapter.Update(lTQNDataSet.Dictionary);
            MessageBox.Show("Cập nhật thành công");
            treeTuDien.Refresh();
        }

        private void treeTuDien_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            //Console.WriteLine(e.Node.GetDisplayText("IndexName"));
            //txtEditChiMuc.DataBindings.Clear();
            //txtEditMucCha.DataBindings.Clear();
            if (rgEditChoice.SelectedIndex == 0 && txtEditChiMuc.DataBindings.Count==0)
            {
                this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.dictionaryBindingSource, "IndexName", true));
                this.txtEditChiMuc.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.dictionaryBindingSource, "ID", true));
            }
            else if (rgEditChoice.SelectedIndex == 1 && txtEditMucCha.DataBindings.Count==0)
            {
                this.txtEditMucCha.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.dictionaryBindingSource, "IndexName", true));
                this.txtEditMucCha.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.dictionaryBindingSource, "ID", true));
            }
            Console.WriteLine(txtEditChiMuc.Tag + "," + txtEditMucCha.Tag);
        }

        private void chkEditMucGoc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEditMucGoc.Checked)
            {
                txtEditMucCha.Visible = false;
                rgEditChoice.Visible = false;
                lblEditChiMuc.Visible = true;
            }
            else
            {
                txtEditMucCha.Visible = true;
                rgEditChoice.Visible = true;
                lblEditChiMuc.Visible = false;

            }
        }


    }
}